{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alessandro Parisi \n",
      "last updated: 2019-04-02 \n",
      "\n",
      "CPython 3.5.4\n",
      "IPython 6.1.0\n",
      "\n",
      "numpy 1.15.2\n",
      "pandas 0.20.3\n",
      "matplotlib 2.2.2\n",
      "sklearn 0.20.0\n",
      "seaborn 0.8.0\n"
     ]
    }
   ],
   "source": [
    "%load_ext watermark\n",
    "%watermark -a \"Alessandro Parisi\" -u -d -v -p numpy,pandas,matplotlib,sklearn,seaborn\n",
    "# to install watermark launch 'pip install watermark' at command line\n",
    "import warnings \n",
    "warnings.simplefilter('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from sklearn.linear_model import *\n",
    "from sklearn.tree import *\n",
    "from sklearn.naive_bayes import *\n",
    "from sklearn.neighbors import *\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "# Load the data.\n",
    "dataset = pd.read_csv('../datasets/network-logs.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "samples = dataset.iloc[:, [1, 2]].values\n",
    "targets = dataset['ANOMALY'].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "training_samples, testing_samples, training_targets, testing_targets = train_test_split(\n",
    "         samples, targets, test_size=0.3, random_state=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "K-Nearest Neighbours accuracy: 95.90163934426229\n"
     ]
    }
   ],
   "source": [
    "# k-Nearest Neighbors model\n",
    "knc = KNeighborsClassifier(n_neighbors=2)\n",
    "knc.fit(training_samples,training_targets)\n",
    "knc_prediction = knc.predict(testing_samples)\n",
    "knc_accuracy = 100.0 * accuracy_score(testing_targets, knc_prediction)\n",
    "print (\"K-Nearest Neighbours accuracy: \" + str(knc_accuracy))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Decision Tree accuracy: 96.72131147540983\n"
     ]
    }
   ],
   "source": [
    "# Decision tree model\n",
    "dtc = DecisionTreeClassifier(random_state=0)\n",
    "dtc.fit(training_samples,training_targets)\n",
    "dtc_prediction = dtc.predict(testing_samples)\n",
    "dtc_accuracy = 100.0 * accuracy_score(testing_targets, dtc_prediction)\n",
    "print (\"Decision Tree accuracy: \" + str(dtc_accuracy))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gaussian Naive Bayes accuracy: 98.36065573770492\n"
     ]
    }
   ],
   "source": [
    "# Gaussian Naive Bayes model\n",
    "gnb = GaussianNB()\n",
    "gnb.fit(training_samples,training_targets)\n",
    "gnb_prediction = gnb.predict(testing_samples)\n",
    "gnb_accuracy = 100.0 * accuracy_score(testing_targets, gnb_prediction)\n",
    "print (\"Gaussian Naive Bayes accuracy: \" + str(gnb_accuracy))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Py35",
   "language": "python",
   "name": "py35"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
